﻿/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 3/23/2010
 * Time: 11:05 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

namespace popLib.CustomeControl
{
	/// <summary>
	/// Description of PageViewer.
	/// </summary>
	public partial class PageViewer : UserControl
	{
		public delegate void PageChangedEvent(int page);
        public event PageChangedEvent PageChanged;
        public delegate void PageRefreshEvent(int page);
        public event PageRefreshEvent PageRefresh;
                
        public PageViewer()
        {
            InitializeComponent();
            generatePages();
            
        }

        private string pageTitle = "Page ([TOTALPAGES])";
        /// <summary>
        /// Page title that shown on the left handside with some replacement keywords.
        /// [TOTALPAGES]: replaced by TotalPages property.
        /// [CURRENTPAGE]: replaced by CurrentPage property.
        /// </summary>
        public string PageTittle {
            get { return pageTitle; }
            set { pageTitle = value; updatePageTitle(); }
        }

        private int totalPages = 1;
        public int TotalPages {
            get { return totalPages; }
            set { totalPages = value; generatePages(); }
        }

        private int currentPage = 1;
        public int CurrentPage
        {
            get { return currentPage; }
            set { 
                currentPage = value;
                if (currentPage > totalPages) currentPage = totalPages;
                if (currentPage < 1) currentPage = 1;
                updateCurrentPage(); 
            }
        }

        void generatePages() {
            cbx_page.Items.Clear();
            currentPage = 1;

            updatePageTitle();

            cbx_page.SelectedIndexChanged -= new EventHandler(cbx_page_SelectedIndexChanged);
            
            for (int i = 1; i <= totalPages; i++) cbx_page.Items.Add( StringLib.spaceLeftInclude(i+"", 6 ) );

            updateCurrentPage();

            cbx_page.SelectedIndexChanged += new EventHandler(cbx_page_SelectedIndexChanged);
        }

        void cbx_page_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentPage = NumberLib.getInt( cbx_page.SelectedIndex+1 );
            if (PageChanged != null)
                PageChanged(currentPage);
            updateButtonState();
        }

        void updatePageTitle() {
            label4.Text = pageTitle.Replace("[TOTALPAGES]", totalPages+"").Replace("[CURRENTPAGE]", currentPage+"");
        }

        void updateCurrentPage() {
            if (currentPage-1 < cbx_page.Items.Count)
                cbx_page.SelectedIndex = currentPage-1;

            updateButtonState();
        }

        void updateButtonState() {
            btn_last.Enabled = btn_next.Enabled = (currentPage < totalPages);
            btn_first.Enabled = btn_previous.Enabled = (currentPage > 1);
        }

        private void ActionEvent(object sender, EventArgs e)
        {
            switch (((Control)sender).Name)
            {
                case "btn_first": if (cbx_page.SelectedIndex > 0) cbx_page.SelectedIndex = 0; break;
                case "btn_previous": if (cbx_page.SelectedIndex > -1) cbx_page.SelectedIndex--; break;
                case "btn_next": if (cbx_page.SelectedIndex < totalPages-1) cbx_page.SelectedIndex++; break;
                case "btn_last": if (cbx_page.SelectedIndex != totalPages - 1) cbx_page.SelectedIndex = totalPages - 1; break;
                case "btn_refresh": if (PageRefresh != null) PageRefresh(currentPage); break;
            }
        }
	}
	
	public class PageData {
        public PageData() { }

        public PageData(int currentPage, int totalPages, object dataSource)
        {
            CurrentPage = currentPage;
            TotalPages = totalPages;
            DataSource = dataSource;
        }

        public PageData(int currentPage, int totalPages, object dataSource, int totalRecords)
        {
            CurrentPage = currentPage;
            DataSource = dataSource;
            TotalPages = totalPages;
            TotalRecords = totalRecords;
        }

        public int CurrentPage { get; set; }

        public int TotalPages { get; set; }

        public int TotalRecords { get; set; }

        public object DataSource { get; set; }
    }
}
